﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace MathematicallySafe.HeadCounter.Data.Common
{
    public static class Helpers
    {
        public static void ForEach<T>(this IEnumerable<T> source, Action<T> func)
        {
            foreach (T item in source)
            {
                func(item);
            }
        }

        public static bool TryParse<T>(this Enum theEnum, string value, out T parsedValue)
        {
            parsedValue = default(T);

            int intEnumValue;
            if (Int32.TryParse(value, out intEnumValue))
            {
                if (Enum.IsDefined(typeof (T), intEnumValue))
                {
                    parsedValue = (T) (object) intEnumValue;
                    return true;
                }
            }

            return false;
        }
    }
}